package system.service;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.ParseException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import system.common.Arguments;
import system.common.Constants;
import system.dao.DB3_DbIDao;
import system.model.DB3_DbIDto;



import collector.core.CollectConstants;
import collector.core.TaskConstVO;
import collector.core.util.DateUtil;
import collector.core.util.FileUtil;
import collector.task.JobStateVO;

/**
 * <pre>
 * Class : system.service.DB3_DbIService / DB3_DbIService.java
 * 	Define Class Object : 
 * <br>
 * </pre>
 * @author hwanLee / apexcns,Inc.
 * @version 1.2
 * @see javadoc
 * @since 2012. 9. 10.
 */
@Service
public class DB3_DbIService {
	@Autowired
	private DB3_DbIDao db3_DbIDao;

	public JobStateVO runDbI(Arguments arguments, JobStateVO pjsVo, TaskConstVO tcVo) throws IOException, ParseException {
	    JobStateVO jsVo = pjsVo;
	    jsVo.setJobState(CollectConstants.CODE_JOB_STATE_DOING);
	    ExportService.OutputStateLog(jsVo);
	    ExportService.getNextSchedule(jsVo, tcVo, arguments);

		String fileNameSuffix = arguments.getParameter1() + "_" + arguments.getFileName1() + "_" + arguments.getFileName2();
		String historyFileName = tcVo.getTaskPathHistory() + tcVo.getTaskName() + "_" + DateUtil.dateToYmd(pjsVo.getScheduleTime(),"yyyyMMddHHmmss") + "_" + fileNameSuffix;
		String currentFileName = tcVo.getTaskPathCurrent() + tcVo.getTaskName();
		String dbName = "DBI_LOG_" + arguments.getDBName().substring(0, 8) + "_000000";
		arguments.setDBFullName(dbName);

		// check for directory
		FileUtil.directoryCheck(tcVo.getTaskPathHistory());
		FileUtil.directoryCheck(tcVo.getTaskPathCurrent());
		
		// delete to current file
		FileUtil.deleteFile(tcVo.getTaskName(), tcVo.getTaskPathCurrent());

		long start;
		long end;
		
		File file = null;
		BufferedWriter bufferedWriter = null;

		float totalRsTime = 0;
		float totalFileMakeTime = 0;
		float totalFileCopyTime = 0;
		long totalRecordsCount = 0;
		int totalFilesCount = 0;
		
		// rs start
		start = System.currentTimeMillis();
		
		List<DB3_DbIDto> db_i_list = db3_DbIDao.db_i_01_list(arguments);
		
		totalRecordsCount = db_i_list.size();

		end = System.currentTimeMillis();
		totalRsTime = (end - start) / 1000f;
		// rs end
		// file-make start
		start = System.currentTimeMillis();
		
		file = new File(historyFileName + "_0001" + FileUtil.genFileNumber(0) + Constants.fileNameExtension);
		bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));

		totalFilesCount = ExportService.OutputList(db_i_list, bufferedWriter, historyFileName, "_0001", 0, tcVo.getTaskName());
		
		end = System.currentTimeMillis();
		totalFileMakeTime = (end - start) / 1000f;
		// file-make end
		// file-copy start
		start = System.currentTimeMillis();
		
		FileUtil.copyBytes(tcVo.getTaskPathHistory(), tcVo.getTaskName() + "_" + DateUtil.dateToYmd(pjsVo.getScheduleTime(),"yyyyMMddHHmmss") + "_" + fileNameSuffix, currentFileName, Constants.fileNameExtension);

		end = System.currentTimeMillis();
		totalFileCopyTime = (end - start) / 1000f;
		// file-copy end

		//jsVo.setJobAuto(jobAuto);
		jsVo.setTotalFileCount(totalFilesCount);
		jsVo.setTotalRecordCount(totalRecordsCount);
		jsVo.setRsTime(totalRsTime);
		jsVo.setFileMakeTime(totalFileMakeTime);
		jsVo.setFileCopyTime(totalFileCopyTime);
		
		jsVo.setSuccDoneResult(CollectConstants.CODE_JOB_RESULT_CODE_SUCC, "do Job Succ State...");
		
		// 작업 상태 객체 리턴
		return jsVo;
	}
}
